<template>
  <div class="practice-history-container">
    <div class="history-header">
      <div class="header-content">
        <h1>练习历史记录</h1>
        <p>查看您的练习历史和成长轨迹</p>
      </div>
      <div class="header-actions">
        <el-button @click="goBack">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
      </div>
    </div>
    
    <!-- 统计概览 -->
    <div class="stats-overview">
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><Trophy /></el-icon>
        </div>
        <div class="stat-content">
          <h3>{{ totalSessions }}</h3>
          <p>总练习次数</p>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><Clock /></el-icon>
        </div>
        <div class="stat-content">
          <h3>{{ totalTime }}h</h3>
          <p>总练习时长</p>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><DataAnalysis /></el-icon>
        </div>
        <div class="stat-content">
          <h3>{{ averageScore }}分</h3>
          <p>平均得分</p>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><TrendCharts /></el-icon>
        </div>
        <div class="stat-content">
          <h3>{{ improvementRate }}%</h3>
          <p>进步幅度</p>
        </div>
      </div>
    </div>
    
    <!-- 筛选器 -->
    <div class="filter-section">
      <div class="filter-row">
        <div class="filter-group">
          <label>练习阶段:</label>
          <el-select v-model="filters.stage" multiple placeholder="选择阶段" @change="filterRecords">
            <el-option label="逻辑能力" value="logic" />
            <el-option label="创新能力" value="innovation" />
            <el-option label="专业技能" value="professional" />
            <el-option label="压力应对" value="pressure" />
          </el-select>
        </div>
        
        <div class="filter-group">
          <label>练习模式:</label>
          <el-select v-model="filters.mode" placeholder="选择模式" @change="filterRecords">
            <el-option label="全部" value="" />
            <el-option label="快速练习" value="quick" />
            <el-option label="综合练习" value="comprehensive" />
            <el-option label="专项练习" value="targeted" />
          </el-select>
        </div>
        
        <div class="filter-group">
          <label>时间范围:</label>
          <el-date-picker
            v-model="filters.dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="filterRecords"
          />
        </div>
      </div>
    </div>
    
    <!-- 练习记录列表 -->
    <div class="records-section">
      <div class="records-header">
        <h3>练习记录</h3>
        <div class="view-options">
          <el-button-group>
            <el-button 
              :type="viewMode === 'list' ? 'primary' : 'default'"
              @click="viewMode = 'list'"
            >
              <el-icon><List /></el-icon>
              列表
            </el-button>
            <el-button 
              :type="viewMode === 'card' ? 'primary' : 'default'"
              @click="viewMode = 'card'"
            >
              <el-icon><Grid /></el-icon>
              卡片
            </el-button>
          </el-button-group>
        </div>
      </div>
      
      <!-- 列表视图 -->
      <div v-if="viewMode === 'list'" class="records-list">
        <el-table :data="filteredRecords" stripe style="width: 100%">
          <el-table-column prop="date" label="练习日期" width="180">
            <template #default="scope">
              {{ formatDate(scope.row.date) }}
            </template>
          </el-table-column>
          <el-table-column prop="stage" label="练习阶段" width="120">
            <template #default="scope">
              <el-tag :type="getStageTagType(scope.row.stage)">
                {{ getStageText(scope.row.stage) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="mode" label="练习模式" width="120">
            <template #default="scope">
              {{ getModeText(scope.row.mode) }}
            </template>
          </el-table-column>
          <el-table-column prop="duration" label="用时" width="80">
            <template #default="scope">
              {{ formatDuration(scope.row.duration) }}
            </template>
          </el-table-column>
          <el-table-column prop="score" label="得分" width="80">
            <template #default="scope">
              <span :class="getScoreClass(scope.row.score)">
                {{ scope.row.score }}分
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="correctRate" label="正确率" width="100">
            <template #default="scope">
              <el-progress 
                :percentage="scope.row.correctRate" 
                :stroke-width="4"
                :show-text="false"
                :color="getProgressColor(scope.row.correctRate)"
              />
              <span class="correct-rate-text">{{ scope.row.correctRate }}%</span>
            </template>
          </el-table-column>
          <el-table-column prop="improvement" label="进步情况" width="120">
            <template #default="scope">
              <span :class="getImprovementClass(scope.row.improvement)">
                {{ scope.row.improvement > 0 ? '+' : '' }}{{ scope.row.improvement }}分
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button 
                type="primary" 
                size="small" 
                @click="viewDetails(scope.row)"
              >
                查看详情
              </el-button>
              <el-button 
                type="text" 
                size="small" 
                @click="reviewAnswers(scope.row)"
              >
                复习答案
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <!-- 卡片视图 -->
      <div v-else class="records-cards">
        <div v-for="record in filteredRecords" :key="record.id" class="record-card">
          <div class="card-header">
            <div class="record-info">
              <el-tag :type="getStageTagType(record.stage)" size="small">
                {{ getStageText(record.stage) }}
              </el-tag>
              <span class="record-date">{{ formatDate(record.date) }}</span>
            </div>
            <div class="record-score">
              <span :class="getScoreClass(record.score)">{{ record.score }}分</span>
            </div>
          </div>
          
          <div class="card-content">
            <div class="record-stats">
              <div class="stat-item">
                <span class="stat-label">练习模式:</span>
                <span class="stat-value">{{ getModeText(record.mode) }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">用时:</span>
                <span class="stat-value">{{ formatDuration(record.duration) }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">正确率:</span>
                <span class="stat-value">{{ record.correctRate }}%</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">题目数:</span>
                <span class="stat-value">{{ record.totalQuestions }}题</span>
              </div>
            </div>
            
            <div class="progress-bar">
              <el-progress 
                :percentage="record.correctRate" 
                :stroke-width="6"
                :color="getProgressColor(record.correctRate)"
              />
            </div>
          </div>
          
          <div class="card-footer">
            <div class="improvement-indicator">
              <span :class="getImprovementClass(record.improvement)">
                {{ record.improvement > 0 ? '进步' : record.improvement < 0 ? '退步' : '持平' }}
                {{ record.improvement !== 0 ? Math.abs(record.improvement) + '分' : '' }}
              </span>
            </div>
            
            <div class="card-actions">
              <el-button type="primary" size="small" @click="viewDetails(record)">
                查看详情
              </el-button>
              <el-button type="text" size="small" @click="reviewAnswers(record)">
                复习答案
              </el-button>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          :total="filteredRecords.length"
          layout="prev, pager, next, sizes, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  ArrowLeft, 
  Trophy, 
  Clock, 
  DataAnalysis, 
  TrendCharts, 
  List, 
  Grid 
} from '@element-plus/icons-vue'

export default {
  name: 'PracticeHistoryView',
  components: {
    ArrowLeft,
    Trophy,
    Clock,
    DataAnalysis,
    TrendCharts,
    List,
    Grid
  },
  setup() {
    const router = useRouter()
    
    // 响应式数据
    const viewMode = ref('list')
    const currentPage = ref(1)
    const pageSize = ref(10)
    
    const filters = reactive({
      stage: [],
      mode: '',
      dateRange: []
    })
    
    // 统计数据
    const totalSessions = ref(45)
    const totalTime = ref(28)
    const averageScore = ref(82)
    const improvementRate = ref(15)
    
    // 模拟练习记录数据
    const practiceRecords = ref([
      {
        id: 1,
        date: '2024-01-20T10:30:00',
        stage: 'logic',
        mode: 'quick',
        duration: 900, // 15分钟
        score: 85,
        correctRate: 80,
        totalQuestions: 10,
        correctAnswers: 8,
        improvement: 5
      },
      {
        id: 2,
        date: '2024-01-19T14:15:00',
        stage: 'innovation',
        mode: 'comprehensive',
        duration: 2700, // 45分钟
        score: 78,
        correctRate: 75,
        totalQuestions: 12,
        correctAnswers: 9,
        improvement: -2
      },
      {
        id: 3,
        date: '2024-01-18T16:45:00',
        stage: 'professional',
        mode: 'targeted',
        duration: 1800, // 30分钟
        score: 92,
        correctRate: 90,
        totalQuestions: 8,
        correctAnswers: 7,
        improvement: 8
      },
      {
        id: 4,
        date: '2024-01-17T09:20:00',
        stage: 'pressure',
        mode: 'quick',
        duration: 1200, // 20分钟
        score: 76,
        correctRate: 70,
        totalQuestions: 10,
        correctAnswers: 7,
        improvement: 0
      },
      {
        id: 5,
        date: '2024-01-16T11:00:00',
        stage: 'logic',
        mode: 'comprehensive',
        duration: 2100, // 35分钟
        score: 88,
        correctRate: 85,
        totalQuestions: 15,
        correctAnswers: 13,
        improvement: 3
      }
    ])
    
    // 计算属性
    const filteredRecords = computed(() => {
      let records = practiceRecords.value
      
      // 按阶段筛选
      if (filters.stage.length > 0) {
        records = records.filter(record => filters.stage.includes(record.stage))
      }
      
      // 按模式筛选
      if (filters.mode) {
        records = records.filter(record => record.mode === filters.mode)
      }
      
      // 按时间范围筛选
      if (filters.dateRange.length === 2) {
        const [start, end] = filters.dateRange
        records = records.filter(record => {
          const recordDate = new Date(record.date)
          return recordDate >= new Date(start) && recordDate <= new Date(end)
        })
      }
      
      return records.sort((a, b) => new Date(b.date) - new Date(a.date))
    })
    
    // 方法
    const goBack = () => {
      router.go(-1)
    }
    
    const filterRecords = () => {
      currentPage.value = 1
    }
    
    const formatDate = (dateString) => {
      return new Date(dateString).toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
    
    const formatDuration = (seconds) => {
      const minutes = Math.floor(seconds / 60)
      const remainingSeconds = seconds % 60
      return `${minutes}分${remainingSeconds}秒`
    }
    
    const getStageTagType = (stage) => {
      const typeMap = {
        'logic': 'primary',
        'innovation': 'success',
        'professional': 'warning',
        'pressure': 'danger'
      }
      return typeMap[stage] || 'info'
    }
    
    const getStageText = (stage) => {
      const textMap = {
        'logic': '逻辑能力',
        'innovation': '创新能力',
        'professional': '专业技能',
        'pressure': '压力应对'
      }
      return textMap[stage] || stage
    }
    
    const getModeText = (mode) => {
      const textMap = {
        'quick': '快速练习',
        'comprehensive': '综合练习',
        'targeted': '专项练习'
      }
      return textMap[mode] || mode
    }
    
    const getScoreClass = (score) => {
      if (score >= 90) return 'score-excellent'
      if (score >= 80) return 'score-good'
      if (score >= 70) return 'score-average'
      return 'score-poor'
    }
    
    const getProgressColor = (rate) => {
      if (rate >= 90) return '#67c23a'
      if (rate >= 80) return '#409eff'
      if (rate >= 70) return '#e6a23c'
      return '#f56c6c'
    }
    
    const getImprovementClass = (improvement) => {
      if (improvement > 0) return 'improvement-positive'
      if (improvement < 0) return 'improvement-negative'
      return 'improvement-neutral'
    }
    
    const viewDetails = (record) => {
      router.push({
        path: '/candidate/practice-detail',
        query: { id: record.id }
      })
    }
    
    const reviewAnswers = (record) => {
      router.push({
        path: '/candidate/practice-review',
        query: { id: record.id }
      })
    }
    
    const handleSizeChange = (size) => {
      pageSize.value = size
    }
    
    const handlePageChange = (page) => {
      currentPage.value = page
    }
    
    return {
      viewMode,
      currentPage,
      pageSize,
      filters,
      totalSessions,
      totalTime,
      averageScore,
      improvementRate,
      practiceRecords,
      filteredRecords,
      goBack,
      filterRecords,
      formatDate,
      formatDuration,
      getStageTagType,
      getStageText,
      getModeText,
      getScoreClass,
      getProgressColor,
      getImprovementClass,
      viewDetails,
      reviewAnswers,
      handleSizeChange,
      handlePageChange
    }
  }
}
</script>

<style scoped>
.practice-history-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 24px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content h1 {
  color: #333;
  margin: 0 0 8px 0;
}

.header-content p {
  color: #666;
  margin: 0;
}

.stats-overview {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #409eff;
}

.stat-content h3 {
  color: #333;
  margin: 0 0 4px 0;
  font-size: 24px;
  font-weight: 600;
}

.stat-content p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.filter-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.filter-row {
  display: flex;
  gap: 20px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-group label {
  color: #333;
  font-weight: 500;
  min-width: 80px;
}

.records-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.records-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.records-header h3 {
  color: #333;
  margin: 0;
}

.records-list {
  margin-bottom: 20px;
}

.correct-rate-text {
  font-size: 12px;
  color: #666;
  margin-left: 8px;
}

.score-excellent { color: #67c23a; font-weight: 600; }
.score-good { color: #409eff; font-weight: 600; }
.score-average { color: #e6a23c; font-weight: 600; }
.score-poor { color: #f56c6c; font-weight: 600; }

.improvement-positive { color: #67c23a; font-weight: 600; }
.improvement-negative { color: #f56c6c; font-weight: 600; }
.improvement-neutral { color: #666; }

.records-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.record-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 16px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.record-card:hover {
  border-color: #409eff;
  background: white;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.record-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.record-date {
  color: #666;
  font-size: 12px;
}

.record-score {
  font-size: 18px;
  font-weight: 600;
}

.card-content {
  margin-bottom: 16px;
}

.record-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}

.stat-label {
  color: #666;
}

.stat-value {
  color: #333;
  font-weight: 500;
}

.progress-bar {
  margin-bottom: 16px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.improvement-indicator {
  font-size: 12px;
}

.card-actions {
  display: flex;
  gap: 8px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-overview {
    grid-template-columns: 1fr;
  }
  
  .filter-row {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .records-cards {
    grid-template-columns: 1fr;
  }
}
</style> 